﻿using System;
using System.Collections.Generic;
using System.Text;

namespace 双人对战游戏服务端
{
    public class MsgHandler
    {
        /// <summary>
        /// 服务端处理收到进入游戏的数据 并记录 然后再广播出去
        /// </summary>
        /// <param name="c"></param>
        /// <param name="msgArgs"></param>
        public static void MsgEnter(ClientState c,string msgArgs)
        {
            //解析参数
            Console.WriteLine("MsgEnter"+msgArgs);
            string[] split = msgArgs.Split(',');
            string desc = split[0];
            float x = float.Parse(split[1]);
            float y =float.Parse(split[2]);
            float z = float.Parse(split[3]);
            float eulY = float.Parse(split[4]);
            //赋值
            c.hp = 100;
            c.x = x;
            c.y = y;
            c.z = z;
            c.eulY = eulY;
            //广播
            string sendStr = "Enter|" + msgArgs;
            foreach (ClientState cs in Program.clients.Values)
            {
                Program.Send(cs,sendStr);
            }

        }

        public static void MsgMove(ClientState C, string msgArgs)
        {

            //解析参数
            string[] split = msgArgs.Split(',');
            string desc = split[0];
            float x = float.Parse(split[1]);
            float y = float.Parse(split[2]);
            float z = float.Parse(split[3]);
            C.x = x;
            C.y = y;
            C.z = z;
            //广播
            string sendStr = "Move|" + msgArgs;
            foreach (ClientState cs in Program.clients.Values)
            {
                Program.Send(cs,sendStr);
            }
        }

        public static void MsgList(ClientState c, string msgArgs)
        {
            string sendStr = "List|";
            foreach (ClientState cs in Program.clients.Values)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(cs.socket.RemoteEndPoint.ToString()).Append(",");
                sb.Append(cs.x.ToString()).Append(",");
                sb.Append(cs.y.ToString()).Append(",");
                sb.Append(cs.z.ToString()).Append(",");
                sb.Append(cs.eulY.ToString()).Append(",");
                sb.Append(cs.hp.ToString()).Append(",");
                sendStr += sb.ToString();
            }
            Program.Send(c,sendStr);

        }

        public static void MsgAttack(ClientState c, string msgArgs)
        {
            //广播
            string sendStr = "Attack|" + msgArgs;
            foreach (ClientState cs in Program.clients.Values)
            {
                Program.Send(cs,sendStr);
            }
        }

        public static void MsgHit(ClientState c, string msgArgs)
        {
            string[] split = msgArgs.Split(',');
            string attDesc = split[0];
            string hitDesc = split[1];
            //找出被攻击的角色
            ClientState hitCs = null;
            foreach (ClientState cs in Program.clients.Values)
            {
                if (cs.socket.RemoteEndPoint.ToString() == hitDesc)
                {
                    hitCs = cs;
                }
            }

            if (hitCs == null)
                return;
            //扣血
            hitCs.hp -= 25;
            //死亡
            if (hitCs.hp <= 0)
            {
                string sendStr = "Die|" + hitCs.socket.RemoteEndPoint.ToString();
                foreach (ClientState state in Program.clients.Values)
                {
                    Program.Send(state,sendStr);
                }

            }


            
        
        }
    }
}